package cn.com.citydo.base.util;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

public class RequestUtil {

	private RequestUtil() {
	}

	public static HttpServletRequest getRequest(){
		return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
	}
	
	public static void setAttribute(String key,Object value){
		getRequest().setAttribute(key, value);
	}
	
	public static boolean isAjax(){
		HttpServletRequest request = RequestUtil.getRequest();
		String requestType = request.getHeader("X-Requested-With");  
		return "XMLHttpRequest".equalsIgnoreCase(requestType)&&!"ajax".equals(request.getParameter("ace"));
	}
	
	public static boolean fromAce(){
		return "ajax".equals(getRequest().getParameter("ace"));
	}
	
	public static String getRequestURL(){
		HttpServletRequest request = getRequest();
		//获取请求URI
        StringBuffer requestPage = request.getRequestURL(); 
        //获取参数
        String queryString = (request.getQueryString() == null ? "" : "?"+request.getQueryString()); 
        return requestPage+queryString;
	}
	
	public static String getIpAddr()  {  
		HttpServletRequest request = getRequest();
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		if(ip.contains(",")){
			ip = ip.split(",")[0];
		}
		if(!ip.contains(".")){
			ip = "0.0.0.0";
		}
		return ip;
	}
	
	public static String getParameter(String key){
		String parameter = getRequest().getParameter(key);
		if(parameter==null){
			return null;
		}
		return parameter.trim();
	}
	public static String[] getParameterValues(String key){
		return getRequest().getParameterValues(key);
	}
	
	public static Long getLongParameter(String key){
		try{
			String strVal = getRequest().getParameter(key);
			return Long.parseLong(strVal);
		}catch(Exception e){
			return null;
		}
	}
	
	public static Integer getIntegerParameter(String key){
		try{
			String strVal = getRequest().getParameter(key);
			return Integer.parseInt(strVal);
		}catch(Exception e){
			return null;
		}
	}

	public static BigDecimal getBigDecimalParameter(String key){
		try{
			String strVal = getRequest().getParameter(key);
			return new BigDecimal(strVal);
		}catch(Exception e){
			return null;
		}
	}

	public static Double getDoubleParameter(String key){
		try{
			String strVal = getRequest().getParameter(key);
			return Double.parseDouble(strVal);
		}catch(Exception e){
			return null;
		}
	}
	
	public static Boolean getBooleanParameter(String key){
		try{
			String strVal = getRequest().getParameter(key);
			if(StringUtils.isBlank(strVal)){
				return null;
			}
			return Boolean.parseBoolean(strVal.trim());
		}catch(Exception e){
			return null;
		}
	}
	
	public static Date getDateParameter(String key,String formart){
		try{
			String strVal = getRequest().getParameter(key);
			if(strVal==null){
				return null;
			}
			return new SimpleDateFormat(formart).parse(strVal.trim());
		}catch(Exception e){
			return null;
		}
	}
	
	public static List<Long> getLongParameters(String key){
		List<Long> list = new ArrayList<>();
		String[] parameterValues = getRequest().getParameterValues(key);
		if(parameterValues==null){
			return list;
		}
		for(String val: parameterValues){
			try{
				list.add(Long.parseLong(val));
			}catch(Exception e){
			}
		}
		return list;
	}


	public static List<String> getSplitParameters(String key){
		String idsStr = RequestUtil.getParameter(key);
		if(StringUtils.isBlank(idsStr)){
			return new ArrayList<>();
		}
		String[] idArr = idsStr.split(",");
		return Arrays.asList(idArr);
	}

	public static List<String> getParameters(String key){
		List<String> list = new ArrayList<>();
		String[] parameterValues = getRequest().getParameterValues(key);
		if(parameterValues==null){
			return list;
		}
		for(String val: parameterValues){
			if(StringUtils.isBlank(val)){
				continue;
			}
			try{
				list.add(val);
			}catch(Exception e){
			}
		}
		return list;
	}
}
